from info import db
from info.models import User
from manage import app
import datetime
import random


def add_test_users():
    users = []
    now = datetime.datetime.now()
    for num in range(0, 10000):
        new_num = num + 20000
        try:
            user = User()
            user.nick_name = "%011d" % new_num
            user.mobile = "%011d" % new_num
            user.password_hash = "pbkdf2:sha256:50000$3muEknOA$ae665d72a487743bab350a4cf020afc909445b879ef4098ed296fbae8926662d"
            t = now - datetime.timedelta(seconds=random.randint(0, 2678400))
            user.last_login = t
            users.append(user)
            print(user.mobile)
        except Exception as e:
            print(e)
    # 手动开启一个app的上下文
    with app.app_context():
        db.session.add_all(users)
        db.session.commit()
    print('OK')

if __name__ == '__main__':
    add_test_users()
